/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package symulator8051;

/**
 *
 * @author Wojtek
 */
public class Utils {

	public static short getValueFromHex(String hex) {
		hex = hex.substring(1);
		int val = Integer.parseInt(hex, 16);
		return (short) val;
	}

	public static short or(short a, short b) {
		String s1 = Integer.toBinaryString(a);
		String s2 = Integer.toBinaryString(b);

		while (s1.length() < 8) {
			s1 = "0" + s1;
		}

		while (s2.length() < 8) {
			s2 = "0" + s2;
		}
		
		String out = "";

		for (int j = 0; j < s1.length(); j++) {
			if(s1.charAt(j) == '1' || s2.charAt(j) == '1'){
				out = out + "1";
			} else {
				out = out + "0";
			}
		}

		return (short) (Integer.parseInt(out, 2));
	}
	
	public static short and(short a, short b) {
		String s1 = Integer.toBinaryString(a);
		String s2 = Integer.toBinaryString(b);

		while (s1.length() < 8) {
			s1 = "0" + s1;
		}

		while (s2.length() < 8) {
			s2 = "0" + s2;
		}
		
		String out = "";

		for (int j = 0; j < s1.length(); j++) {
			if(s1.charAt(j) == '1' && s2.charAt(j) == '1'){
				out = out + "1";
			} else {
				out = out + "0";
			}
		}

		return (short) (Integer.parseInt(out, 2));
	}
}
